/**
 * @Description: axios封装
 */
import axios from 'axios'
import config from './config'

// let VUE_APP_BASE_API=config.config.qcyFrameTest+'manager/'
let VUE_APP_BASE_API=config.config.qingdaoBigModel

axios.defaults.baseURL = VUE_APP_BASE_API
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.defaults.headers.post['X-Auth-Token'] = localStorage.getItem("token") || ''

// axios.interceptors.request.use(
//   config => {
//     let token = localStorage.getItem("token") || ''
//       config.headers['X-Auth-Token'] = token
//     return config
//   },
//   error => {
//     Promise.resolve(error)
//   }
// )

axios.defaults.timeout = 1000


function get(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err.data)
    })
  })
}

function post(url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}

function put(url, params) {
  return new Promise((resolve, reject) => {
    axios.put(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}

function patch(url, params) {
  return new Promise((resolve, reject) => {
    axios.patch(url, params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}

function del(url, params, data) {
  return new Promise((resolve, reject) => {
    axios.delete(url, data ? { params, data } : params)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}

function downloadFile(url, type = 'get', params) {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url, // 请求地址
      responseType: 'blob', // 表明返回服务器返回的数据类型
      data: params
    }).then(
      response => {
        resolve(response)
      },
      err => {
        reject(err)
      }
    )
  })
}

function uploadFile(url, formData, config = { headers: { 'Content-Type': 'multipart/form-data' } }) {
  return axios.post(url, formData, config)
}

function downloadZip(url, params, type = 'post') {
  return new Promise((resolve, reject) => {
    axios({
      method: type,
      url: url, // 请求地址
      responseType: 'blob', // 表明返回服务器返回的数据类型
      data: params
      // headers: {
      //   'Content-Type': 'application/octet-stream;charset=utf-8'
      // }
    }).then(
      response => {
        resolve(response)
      },
      err => {
        reject(err)
      }
    )
  })
}
export default { get, post, put, del, patch, downloadFile, downloadZip, uploadFile }
export { get, post, put, del, patch, downloadFile, downloadZip, uploadFile }

